package com.javaEE.tes.controllers;

import com.javaEE.tes.dao.dto.StudentClass1;
import com.javaEE.tes.entities.Class1;
import com.javaEE.tes.entities.Student;
import com.javaEE.tes.entities.Teacher;
import com.javaEE.tes.services.Class1Service;
import com.javaEE.tes.services.StudentService;
import com.javaEE.tes.services.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("class")
public class Class1Controllers {

    @Autowired
    Class1Service class1Service;
    @Autowired
    StudentService studentService;
    @Autowired
    TeacherService teacherService;


    @GetMapping
    public String displayClass(Model model){

       List<StudentClass1> class1 = class1Service.class1Student();
        model.addAttribute("class",class1);

        return "class/list-class";
    }
    @GetMapping("/new")
    public String displayClassFrom(Model model){
        Class1 class1s = new Class1();
        List<Student> students = studentService.getAll();
        List<Teacher> teachers = teacherService.getAll();
        model.addAttribute("class1", class1s);
        model.addAttribute("aStudent",students);
        model.addAttribute("teacher",teachers);
        return "class/new-class";
    }
    @PostMapping("/save")
    public String createClass(Model model , @Valid Class1 class1, Errors errors){
        List<Teacher> teachers = teacherService.getAll();
        model.addAttribute("teacher",teachers);
        if (errors.hasErrors()) return "class/new-class";
        class1Service.save(class1);
        return "redirect:/class";
    }
}
